package org.xinjiang.data.migration.xinjiang.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xinjiang.data.migration.common.response.Param;
import org.xinjiang.data.migration.common.utils.query.Pages;
import org.xinjiang.data.migration.xinjiang.convert.XjDcpgConvert;
import org.xinjiang.data.migration.xinjiang.dto.req.XjDcpgQueryDTO;
import org.xinjiang.data.migration.xinjiang.dto.res.XjSubDcpgResultDTO;
import org.xinjiang.data.migration.xinjiang.entity.XjDcpgEntity;
import org.xinjiang.data.migration.xinjiang.mapper.XjDcpgMapper;
import org.xinjiang.data.migration.xinjiang.service.IXjDcpgService;

import java.util.List;

@Service
public class XjDcpgServiceImpl extends ServiceImpl<XjDcpgMapper, XjDcpgEntity> implements IXjDcpgService {

    @Autowired
    private XjDcpgConvert xjDcpgConvert;

    @Override
    public IPage<XjSubDcpgResultDTO> findPageList(Param pageable, XjDcpgQueryDTO query) {
        IPage<XjSubDcpgResultDTO> pageList = this.baseMapper.findPageList(Pages.page(pageable), query);
        if (CollectionUtils.isNotEmpty(pageList.getRecords())) {
            this.postAddAttribute(pageList.getRecords());
        }
        return pageList;
    }

    /**
     * 数据经过了sql一次处理，复杂灵活的业务放到程序中进行二次后处理
     *
     * @param recordList 矫正对象及其属性列表
     */
    private void postAddAttribute(List<XjSubDcpgResultDTO> recordList) {
        if (CollectionUtils.isEmpty(recordList)) {
            return;
        }

        for (XjSubDcpgResultDTO record : recordList) {
            xjDcpgConvert.postAddAttribute(record);
        }
    }
}
